package org.yoki.edu.portal.web.rest.portal;

import com.yunzhitx.cloud.common.model.InvokeResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yoki.edu.common.protocol.portal.exception.PortalException;
import org.yoki.edu.common.protocol.portal.v1.sender.PortalV1PapMsgSender;
import org.yoki.edu.portal.core.portal.vo.PortalWifiParamsVo;
import org.yoki.edu.portal.core.radius.domain.Radcheck;
import org.yoki.edu.portal.web.utils.JudgeBrowserUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Sky$
 * @Description: TODO
 * @date 2017/10/27$ 18:09$
 */
@Controller
@RequestMapping("portal")
public class PortalIndexController {

    private static Logger logger = LoggerFactory.getLogger(PortalIndexController.class);

    @Value("${portal.base.url}")
    private String baseUrl;

    @Value("${portal.ac.ip}")
    private String acIp;
    @Value("${portal.ac.port}")
    private Integer acPort;

    @Value("${weixin.mp.appId}")
    private String appId;
    @Value("${weixin.mp.shopId}")
    private String shopId;
    @Value("${weixin.mp.secretKey}")
    private String secretKey;
    @Value("${weixin.mp.wxRedirectUrl}")
    private String wxRedirectUrl;

    @RequestMapping("index")
    public String index(PortalWifiParamsVo vo, HttpServletRequest request, HttpServletResponse response, Model model) {
        if(!JudgeBrowserUtils.judge(request)){
            return "index";
        }

        Map map = request.getParameterMap();
        Iterator<String> iter = map.keySet().iterator();
        System.out.println("--------------------start--------------------");
        System.out.println(request.getHeader("User-Agent"));
        while (iter.hasNext()) {
            String key = iter.next();
            String[] values = (String[]) map.get(key);
            if (null != values && values.length > 0) {
                System.out.println("key：" + key + " ， value：" + values[0]);

            }
        }
        System.out.println("");
        System.out.println(vo.toString());
        System.out.println("---------------------end---------------------");
        System.out.println("");
        vo.setSsid("HFSD_test");
//        vo.setSsid("i-town小镇官方免费wifi");
        if (null == vo) {
            model.addAttribute("error", "用户认证参数获取失败，请在官方Wifi环境下打开！");
            System.out.println("获取认证参数失败！");
            vo = new PortalWifiParamsVo();
        }
        model.addAttribute("params", vo);
//        return "index";
        return "index";
    }

    @RequestMapping("logout")
    public ResponseEntity<InvokeResult> logout(String userName){
        ResponseEntity responseEntity ;
        int serialNo = (int) (65535 * Math.random());
        Radcheck radcheck = Radcheck.selectByUserName(userName) ;
        if(null == radcheck){
            return new ResponseEntity<>(InvokeResult.passNoData() , HttpStatus.CREATED) ;
        }
        PortalV1PapMsgSender sender = new PortalV1PapMsgSender();
        System.out.println("账号准备强制下线，用户名：" + userName);
        try {
            sender.sendAndParseReqLogout(serialNo, radcheck.getLoginIp(), acIp, acPort);
            System.out.println("账号强制下线成功，用户名：" + userName);
            responseEntity =  new ResponseEntity<>(InvokeResult.ok() , HttpStatus.OK) ;
        } catch (IOException e) {
            e.printStackTrace();
            responseEntity =  new ResponseEntity<>(InvokeResult.error() , HttpStatus.INTERNAL_SERVER_ERROR) ;
        } catch (PortalException e) {
            e.printStackTrace();
            responseEntity =  new ResponseEntity<>(InvokeResult.error() , HttpStatus.INTERNAL_SERVER_ERROR) ;
        }
        return responseEntity ;
    }

}
